package com.songlanyun.modules.config.controller.admin;

import com.songlanyun.common.annotation.SysLog;
import com.songlanyun.common.utils.PageUtils;
import com.songlanyun.common.utils.R;
import com.songlanyun.common.validator.ValidatorUtils;
import com.songlanyun.modules.config.entity.SystemSaleBaseConfigEntity;
import com.songlanyun.modules.config.model.SystemSaleBaseConfigDTO;
import com.songlanyun.modules.config.service.SystemSaleBaseConfigService;
import com.songlanyun.modules.user.controller.AbstractController;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;


/**
 * 平台基本配置表
 *
 * @author Mark
 * @email sunlightcs@gmail.com
 * @date 2020-11-12 11:40:28
 */
@RestController
@RequestMapping("admin/config/base")
public class SystemSaleBaseConfigAdminController extends AbstractController {
    @Autowired
    private SystemSaleBaseConfigService systemSaleBaseConfigService;

    /**
     * 列表
     */
    @RequestMapping("/list")
//    @RequiresPermissions("config:systemSaleBaseConfig:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = systemSaleBaseConfigService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
//    @RequiresPermissions("config:systemSaleBaseConfig:info")
    public R info(@PathVariable("id") Integer id) {
        SystemSaleBaseConfigEntity systemSaleBaseConfig = systemSaleBaseConfigService.getById(id);

        return R.ok().put("data", systemSaleBaseConfig);
    }

    /**
     * 保存 /修改
     */
    @SysLog("保存修改平台基本配置")
    @RequestMapping("/save")
//    @RequiresPermissions("config:systemSaleBaseConfig:save")
    public R save(@RequestBody SystemSaleBaseConfigDTO dto) {
        verifyAdmin();//校验用户
        ValidatorUtils.validateEntity(dto);

        if (dto.getId() == null) {
            //保存
            SystemSaleBaseConfigEntity entity = new SystemSaleBaseConfigEntity();
            BeanUtils.copyProperties(dto, entity);//克隆一份
            systemSaleBaseConfigService.save(entity);
        } else {
            //修改
            SystemSaleBaseConfigEntity entity = systemSaleBaseConfigService.getById(dto.getId(), true);
            BeanUtils.copyProperties(dto, entity);//克隆一份
            systemSaleBaseConfigService.updateById(entity);
        }


        return R.ok();
    }


}
